package cn.jy.map.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.jy.map.service.HCTAnalysisService;
import cn.jy.map.utils.DateUtils;
import cn.jy.map.utils.GjzHctResult;
import cn.jy.map.utils.NurseResult;

@Controller
public class HCTAnalysisCntroller extends BaseController {

	@Autowired
	private HCTAnalysisService hCTAnalysisService;

	/**
	 * 护士id
	 */
	@RequestMapping("hct/nurse")
	@ResponseBody
	public List<NurseResult> findCycleByNurse() throws Exception {
		this.getResponse().setContentType("text/html");
		this.getResponse().setCharacterEncoding("utf-8");

		List<NurseResult> nurse = hCTAnalysisService.findCycleByNurse();
		String callback = this.getRequest().getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(nurse);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = this.getResponse().getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return nurse;
	}

	@RequestMapping("hct/update")
	@ResponseBody
	public void update() {

		hCTAnalysisService.update();
	}

	/**
	 * 护士关联浆员的层数
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("hct/state")
	@ResponseBody
	public List<Map<String, String>> findCycleByState(
			@RequestParam("nurse") String nurseId) throws Exception {
		this.getResponse().setContentType("text/html");
		this.getResponse().setCharacterEncoding("utf-8");
		List<Map<String, String>> state = hCTAnalysisService
				.findCycleByState(nurseId);
		String callback = this.getRequest().getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(state);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = this.getResponse().getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return state;
	}

	/**
	 * 所属地址
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("hct/address")
	@ResponseBody
	List<Map<String, String>> findCycleByAddress(
			@RequestParam("nurse") String nurseId, String state)
			throws Exception {
		this.getResponse().setContentType("text/html");
		this.getResponse().setCharacterEncoding("utf-8");
		List<Map<String, String>> address = hCTAnalysisService
				.findCycleByAddress(nurseId, state);
		String callback = this.getRequest().getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(address);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = this.getResponse().getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return address;
	}

	@RequestMapping("hct/searchCycles")
	@ResponseBody
	public List<GjzHctResult> findCycles(String nurse, String state,
			String address) throws Exception {
		this.getResponse().setContentType("text/html");
		this.getResponse().setCharacterEncoding("utf-8");
		List<GjzHctResult> result = hCTAnalysisService.findCycles(nurse, state,
				address);
		String callback = this.getRequest().getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(result);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = this.getResponse().getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return result;
	}

	@RequestMapping("hct/connectNurse")
	@ResponseBody
	public Map<String, String> getPlasmaMemberInformation(String providerNo,
			String connectionTime) throws Exception {
		this.getResponse().setContentType("text/html");
		this.getResponse().setCharacterEncoding("utf-8");
		System.err.println("providerNo" + providerNo + "connectionTime"
				+ connectionTime);
		Map<String, String> result = hCTAnalysisService
				.getPlasmaMemberInformation(providerNo, connectionTime);
		String callback = this.getRequest().getParameter("callback");
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(result);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = this.getResponse().getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return result;

	}
//
//	@RequestMapping("hct/test1")
//	@ResponseBody
//	public void test() {
//		hCTAnalysisService.test();
//	}

}
